import { MidwayAppInfo, MidwayConfig } from '@midwayjs/core'
import { uploadWhiteList } from '@midwayjs/upload'
import { redisStore } from 'cache-manager-ioredis-yet'
import { tmpdir } from 'os'
import { join } from 'path'

import { APP_SECRETS } from '@/constant'

import { NamingStrategy } from './naming.strategy'

export default (appInfo: MidwayAppInfo): MidwayConfig => {
  const UPLOAD_PATH = join(tmpdir(), 'midway-upload-files')
  const UPLOAD_WHITE_LIST = ['.xls', '.xlsx', ...uploadWhiteList]

  return {
    jwt: {
      secret: APP_SECRETS.JWT_SECRET,
      expiresIn: 2 * 24 * 60 * 60 * 1000
    },
    typeorm: {
      dataSource: {
        default: {
          type: 'mysql',
          host: '127.0.0.1',
          port: 3306,
          username: 'root',
          password: 'root',
          database: 'midway_admin',
          entities: '/entity',
          namingStrategy: new NamingStrategy()
        }
      }
    },
    redis: {
      client: {
        host: '127.0.0.1',
        port: 6379,
        db: 0
      }
    },
    cacheNext: {
      store: redisStore,
      opts: {
        host: '127.0.0.1',
        port: 6379,
        db: 0
      }
    },
    upload: {
      mode: 'file',
      fileSize: '30mb',
      whitelist: UPLOAD_WHITE_LIST,
      tmpdir: UPLOAD_PATH,
      cleanTimeout: 5 * 60 * 1000
    },
    bull: {
      defaultQueueOptions: {
        redis: {
          host: '127.0.0.1',
          port: 6379,
          db: 0
        },
        prefix: '{midway-bull}'
      }
    }
  }
}
